import {
  defineAsyncComponent,
  FunctionalComponent, CSSProperties, ref
} from 'vue';
import { noop } from '@/utils';
import type { Fn } from 'types/index';

const Loading: FunctionalComponent<{ size: 'small' | 'default' | 'large' }> = (props) => {
  const style: CSSProperties = {
    position: 'absolute',
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
  };

  const loading = ref(true)

  return (
    <div style={style} v-loading={loading}>
      {/* <Spin spinning={true} size={props.size} /> */}
    </div>
  );
};

interface Options {
  size?: 'default' | 'small' | 'large';
  delay?: number;
  timeout?: number;
  loading?: boolean;
  retry?: boolean;
}

export function createAsyncComponent(loader: Fn, options: Options = {}) {
  const { size = 'small', delay = 200, timeout = 30000, loading = false, retry = true } = options;
  return defineAsyncComponent({
    // 加载函数
    loader,
    // 加载异步组件时使用的组件
    loadingComponent: loading ? Loading : undefined,

    // 如果提供了一个 timeout 时间限制，并超时了
    // 也会显示这里配置的报错组件，默认值是：Infinity
    timeout,
    // 展示加载组件前的延迟时间，默认为 200ms
    delay,
    /**
     *
     * @param {*} error Error message object
     * @param {*} retry A function that indicating whether the async component should retry when the loader promise rejects
     * @param {*} fail  End of failure
     * @param {*} attempts Maximum allowed retries number
     */
    onError: !retry
      ? noop
      : (error, retry, fail, attempts) => {
        console.log(error)
        if (error.message.match(/api/) && attempts <= 3) {
          // retry on fetch errors, 3 max attempts
          retry();
        } else {
          // Note that retry/fail are like resolve/reject of a promise:
          // one of them must be called for the error handling to continue.
          fail();
        }
      },
  });
}
